#include "WorldData.h"
#include <math.h>

double WorldData::timeElapsed;

void WorldData::tick(float _deltaSeconds)
{
	timeElapsed += _deltaSeconds;

}

WorldData::WorldData(float _worldWidth, float _worldHeight, float _influenceMapGranularity, float _topLeftPosX, float _topLeftPosY)
{
	timeElapsed = 0.0f;

	//Initialise influence maps
	worldTopLeftXPos = _topLeftPosX;
	worldTopLeftYPos = _topLeftPosY;
	worldMapSizeX = _worldWidth;
	worldMapSizeY = _worldHeight;
	influenceMapGranularity = _influenceMapGranularity;

	int InfluenceMapSizeX = (int)ceil(_worldWidth / _influenceMapGranularity);
	int InfluenceMapSizeY = (int)ceil(_worldHeight / _influenceMapGranularity);

	recentDamage = new InfluenceMap(InfluenceMapSizeX, InfluenceMapSizeY);
	
}

InfluenceMap* WorldData::compileInfluenceMap(InfluenceMap* _map1, InfluenceMap* _map2)
{
	if (_map1 == 0 || _map2 == 0)
		return 0;

	int newSizeX = _map1->getSizeX() < _map2->getSizeX() ? _map1->getSizeX() : _map2->getSizeX();
	int newSizeY = _map1->getSizeY() < _map2->getSizeY() ? _map1->getSizeY() : _map2->getSizeY();

	InfluenceMap* compiledMap = new InfluenceMap(newSizeX, newSizeY);

	unsigned char** map1 = _map1->getMap();
	unsigned char** map2 = _map2->getMap();

	unsigned char mapValue1;
	unsigned char mapValue2;

	for (int x = 0; x < newSizeX; x++)
	{
		for (int y = 0; y < newSizeY; y++)
		{
			//For each reference, bit shift right once, then add together
			mapValue1 = map1[x][y] >> 1;
			mapValue2 = map2[x][y] >> 1;

			compiledMap->setAtUnchecked(x, y, mapValue1 + mapValue2);
		}
	}
	return compiledMap;
}


InfluenceMap* WorldData::compileInfluenceMap(InfluenceMap* _map1, InfluenceMap* _map2, InfluenceMap* _map3, InfluenceMap* _map4)
{
	if (_map1 == 0 || _map2 == 0 || _map3 == 0 || _map4 == 0)
		return 0;
	
	//Get smallest map.  Should be irrelevant, but prevents unexpected buffer overruns
	int newSizeX = _map1->getSizeX() < _map2->getSizeX() ? _map1->getSizeX() : _map2->getSizeX();
	newSizeX = newSizeX < _map3->getSizeX() ? newSizeX : _map3->getSizeX();
	newSizeX = newSizeX < _map4->getSizeX() ? newSizeX : _map4->getSizeX();

	int newSizeY = _map1->getSizeY() < _map2->getSizeY() ? _map1->getSizeY() : _map2->getSizeY();
	newSizeY = newSizeY < _map3->getSizeY() ? newSizeY : _map3->getSizeY();
	newSizeY = newSizeY < _map4->getSizeY() ? newSizeY : _map4->getSizeY();

	InfluenceMap* compiledMap = new InfluenceMap(newSizeX, newSizeY);

	unsigned char** map1 = _map1->getMap();
	unsigned char** map2 = _map2->getMap();
	unsigned char** map3 = _map3->getMap();
	unsigned char** map4 = _map4->getMap();

	unsigned char mapValue1;
	unsigned char mapValue2;
	unsigned char mapValue3;
	unsigned char mapValue4;

	for (int x = 0; x < newSizeX; x++)
	{
		for (int y = 0; y < newSizeY; y++)
		{
			//For each reference, bit shift right once, then add together
			mapValue1 = map1[x][y] >> 2;
			mapValue2 = map2[x][y] >> 2;
			mapValue3 = map3[x][y] >> 2;
			mapValue4 = map4[x][y] >> 2;

			compiledMap->setAtUnchecked(x, y, mapValue1 + mapValue2 + mapValue3 + mapValue4);
		}
	}
	return compiledMap;
}